package com.haliyoo.adhs.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.haliyoo.adhs.common.Constant;
import com.haliyoo.adhs.common.MD5;
import com.haliyoo.adhs.common.ResultCode;
import com.haliyoo.adhs.common.ResultData;
import com.haliyoo.adhs.dao.UserDao;
import com.haliyoo.adhs.exception.DaoLevelException;
import com.haliyoo.adhs.model.User;
import com.haliyoo.adhs.service.UserService;


@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {

    private static final Logger	logger	= Logger.getLogger(UserServiceImpl.class);
    
    private UserDao userDao;

    public ResultData<User> findAll(){
    	ResultData<User> rd = new ResultData<User>();
    	try{
    		
			rd.setDataList(userDao.findAll());
			rd.setCode(ResultCode.SUC.getValue());
	    } catch (DaoLevelException e) {
	    	
	    	rd.setCode(ResultCode.DLE.getValue());
	    	logger.error(e.getMessage(),e);
		}
		return rd;
    }
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public ResultData<String> save(User user){
    	ResultData<String> rd = new ResultData<String>();
    	try{
    		
	    	userDao.save(user);
	    	rd.setCode(ResultCode.SUC.getValue());
	    } catch (DaoLevelException e) {
	    	
	    	rd.setCode(ResultCode.DLE.getValue());
	    	logger.error(e.getMessage(),e);
		}
    	return rd;
    }
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public ResultData<String> delete(User user){
    	ResultData<String> rd = new ResultData<String>();
    	
    	try{
    		
	    	userDao.remove(user);
	    	rd.setCode(ResultCode.SUC.getValue());
	    } catch (DaoLevelException e) {
	    	
	    	rd.setCode(ResultCode.DLE.getValue());
	    	logger.error(e.getMessage(),e);
		}
    	return rd;
    }
    
    public ResultData<String> login(Integer userId,String password){
    	
    	ResultData<String> rd = new ResultData<String>();
    	User user = null;
    	try{
    		user = userDao.find(userId);
    	}catch(DaoLevelException e){
    		rd.setCode(ResultCode.UNF.getValue());
	    	logger.error(e.getMessage(),e);
    	}
    	
    	if(user != null){
    		if((user.getPassword() !=null && user.getPassword().equals(password))
    				||(user.getPassword()==null && password ==null)){
    			
    			rd.setFirstData(MD5.getInstance().getHash(Constant.EncryptKey+userId));
    			rd.putData(userId.toString());
    			rd.setCode(ResultCode.SUC.getValue());
    		}else{
    			rd.setCode(ResultCode.INP.getValue());
    		}
    	}else{
    		rd.setCode(ResultCode.UNF.getValue());
    	}
    	return rd;
    }
    
    public ResultData<String> login(String userName,String password){
    	
    	ResultData<String> rd = new ResultData<String>();
    	User user = null;
    	try{
    		user = userDao.findByName(userName);
    	}catch(DaoLevelException e){
    		rd.setCode(ResultCode.UNF.getValue());
	    	logger.error(e.getMessage(),e);
    	}
    	
    	if(user != null){
    		if((user.getPassword() !=null && user.getPassword().equals(password))
    				||(user.getPassword()==null && password ==null)){
    			
    			rd.setFirstData(MD5.getInstance().getHash(Constant.EncryptKey+user.getUserId()));
    			rd.putData(user.getUserId().toString());
    			rd.setCode(ResultCode.SUC.getValue());
    			
    		}else{
    			rd.setCode(ResultCode.INP.getValue());
    		}
    	}
    	return rd;
    }
    public ResultData<String> login(Integer weiboUserId, String password, Short weiboFlag){
    	ResultData<String> rd = new ResultData<String>();
    	User user = null;
    	try{
    		user = userDao.findByWeiboUserId(weiboUserId, weiboFlag);
    	}catch(DaoLevelException e){
    		rd.setCode(ResultCode.UNF.getValue());
	    	logger.error(e.getMessage(),e);
    	}
    	if(user != null){
    		if((user.getPassword() !=null && user.getPassword().equals(password))
    				||(user.getPassword()==null && password ==null)){
    			
    			rd.setFirstData(MD5.getInstance().getHash(Constant.EncryptKey+user.getUserId()));
    			rd.putData(user.getUserId().toString());
    			rd.setCode(ResultCode.SUC.getValue());
    			
    		}else{
    			rd.setCode(ResultCode.INP.getValue());
    		}
    	}
    	return rd;
    }
    
	public ResultData<String> logout(Integer userId) {
		ResultData<String> rd = new ResultData<String>();
		rd.setCode(ResultCode.SUC.getValue());
		return rd;
	}

    public ResultData<User> findById(int id){
    	
    	ResultData<User> rd = new ResultData<User>();
    	User user = null;
    	
    	try{
    		
    		user = userDao.find(id);
    		rd.setCode(ResultCode.SUC.getValue());
	    }catch(DaoLevelException e){
	    	
    		rd.setCode(ResultCode.UNF.getValue());
	    	logger.error(e.getMessage(),e);
    	}
    	rd.setFirstData(user);
    	
    	return rd;
    }
	public ResultData<User> findByName(String userName){
    	
		ResultData<User> rd = new ResultData<User>();
		User user = null;
		
		try{
			
			user = userDao.findByName(userName);
			rd.setCode(ResultCode.SUC.getValue());
		}catch(DaoLevelException e){
			
    		rd.setCode(ResultCode.UNF.getValue());
	    	logger.error(e.getMessage(),e);
    	}
		rd.setFirstData(user);
		 
		return rd;
    }

	/* GETTERS & SETTERS */
	
    public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

}
